<jsp:useBean id="sendbean" class="dasregistry.sendMailToFriendBean" scope="session"/>

<%@ page contentType="application/x-java-jnlp-file" %>


<%

// if browser == internet explorer, create unique file name
// else tell not to cache.
// this is a work around for know IE and Java Web Start problem 

String agent = request.getHeader("User-Agent");
boolean ieBrowser = false;

if ( agent.indexOf("MSIE") > -1 ) {
    ieBrowser = true;
}

if ( ! ieBrowser ) {
   // do tell the browser not to cache ...
   response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
   response.setHeader("Pragma","no-cache"); //HTTP 1.0
   response.setHeader("Content-Disposition","inline; filename=runspice.jnlp");
   response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
} else {
     char[] allow = "ACDEFGHIJKLMNPQRTUVWXYZ1234679".toCharArray();
     
     String uniqueKey = "";
     java.util.Random generator = new java.util.Random();
     for (int i=0; i<9;i++){
       char nextChar = allow[generator.nextInt(allow.length)];
       uniqueKey += nextChar;
     }

  // IE gets a unique filename
  // some IE versions do not download the file if no-cache is specified and then web start will not work...
  String uniqueFilename = "spice_" + uniqueKey +".jnlp";
   response.setHeader("Content-Disposition","inline; filename="+uniqueFilename);
   response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
}

%>


<% 
   // init


   // set defaults
   String type= "PDB";
   String id="1boi";

   String pdb = request.getParameter("pdb");   
   if ( pdb != null ) {
   	type ="PDB";   
	id = pdb ;	
   }

   String uniprot =request.getParameter("uniprot");
   if ( uniprot != null) {
   	type = "UniProt";
	id = uniprot ;
   }
   
   String ensp = request.getParameter("ensp");
   if ( ensp != null ) {
      type = "ENSP";
      id = ensp;
   }

   if ( ( id == null) || ( id.equals(""))){
      type = "PDB";
      id = "1boi";
   }

   // display DAS sources
   boolean displaySources = false;
   String disps = request.getParameter("display");   

   String spiceSendId = "";
   if ( disps != null ) {
      String[] spl =  disps.split(";");

      if ( spl.length != 0) {

   	/// do some sanity checks...
   	 for ( int i = 0 ; i< spl.length ; i++){
            
            String code = spl[i];
            
            // each code must match to the following pattern:
            // something + "_" + a number.
            
            String[] codespl = code.split("_");
            if ( codespl.length != 2 ) {
	        //logger.warning("DAS-source id does not contain one >_< character " + code);
                continue;
            }
            try {
                int number = Integer.parseInt(codespl[1]);
            } catch (Exception e){
	        //logger.warning("DAS-source id does not contain a number after the >_< " + code);
		continue;
            }
            
            if ( code.length() > 100){
	        //logger.warning("DAS-source id is too long! ("+code.length()+" > 100 chars)");
		continue;
            }
	    displaySources = true;
            spiceSendId += code +";";
        }
      }
   }

   // display DAS label
   boolean displayLabel = false;
   String sentLabel = request.getParameter("displayLabel");
   String spiceSendLabel = "";
   if ( sentLabel != null) {
       String[] spl =  sentLabel.split(";");

       if ( spl.length != 0) {
        for ( int i = 0 ; i< spl.length ; i++){
            String label = spl[i];
            if ( label.length() > 40){
                //logger.warning("Label length is too long! (" + label.length() +">40)");
                return;
            }
	    displayLabel = true;
            spiceSendLabel += label + ";";
        }
      }
   }


   // optional arguments, for interacting with spice from the outside.
   // arguments: rasmolScript, seqSelectStart, seqSelectEnd, pdbSelectStart, pdbSelectEnd, displayMessage, displayMessageWidth, displayMessageHeight.

  
   
   boolean sendRasmolScript = false;
   String rasmolScript =  request.getParameter("rasmolScript");
   if ( rasmolScript != null ) {
   	sendRasmolScript = true;
	// we assume that the string is escaped.
	rasmolScript = sendbean.unescape(rasmolScript);
   }

   


   boolean displayMessage = false;
   String showMessage =  request.getParameter("displayMessage");
   if ( showMessage != null ) {
      displayMessage = true;
      showMessage = sendbean.unescape(showMessage);
   }

   String displayMessageWidth  = request.getParameter("displayMessageWidth");
   String displayMessageHeight = request.getParameter("displayMessageHeight");


   String seqSelectStart = request.getParameter("seqSelectStart");
   String seqSelectEnd   = request.getParameter("seqSelectEnd");

   String pdbSelectStart = request.getParameter("pdbSelectStart");
   String pdbSelectEnd   = request.getParameter("pdbSelectEnd");

   

   // and some more optional arguments to add a locally registered server

   String localServerURL      = request.getParameter("localServerURL");
   if ( localServerURL != null ) {
      localServerURL = sendbean.unescape(localServerURL);
   }

   String localServerCoordSys = request.getParameter("localServerCoordSys");
   if ( localServerCoordSys != null ) {
      localServerCoordSys = sendbean.unescape(localServerCoordSys);      
   }
   String localServerName     = request.getParameter("localServerName");
   if ( localServerName != null ) {
      localServerName = sendbean.unescape(localServerName);
   }



%>
<?xml version="1.0" encoding="utf-8"?>
<jnlp
  spec="1.0+"
  codebase="http://www.dasregistry.org/spice/"
>
  <information>
    <title>SPICE</title>
    <vendor>Sanger Institute</vendor>
    <homepage href="http://www.dasregistry.org/spice/spice.shtml"/>
    <description>A browser for protein sequences, structures and their annotations, 
    based on the DAS protocol.</description>
    <description kind="short">Browser for protein sequences, structures and their 
    annotations.</description>
 
  </information>

  <security>
      <all-permissions/>
  </security>

  <resources>
    <j2se version="1.6 1.5 1.4" initial-heap-size="64m" max-heap-size="256m" />
    <jar href="spice.jar"                    />
    <property name="javax.xml.parsers.SAXParserFactory"
              value="org.apache.xerces.jaxp.SAXParserFactoryImpl"/>
    <property name="javax.xml.parsers.DocumentBuilderFactory" 
              value="org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"/>
    <property name="sun.java2d.opengl"
	      value="true"/>
    <property name="apple.laf.useScreenMenuBar"
	      value="true"/>

  </resources>

  <application-desc main-class="org.biojava.spice.Spice">

    <!-- currently supported codetypes: UniProt and PDB -->
    <argument>-codetype</argument>
    <argument><%=type%></argument>
    
    <argument>-code</argument>
    <argument><%=id%></argument>
   

    <% if (displaySources) {
      %>
      <argument>-display</argument>
      <argument><%=spiceSendId%></argument>
      <%
    }
    if (displayLabel) {
      %>
      <argument>-displayLabel</argument>
      <argument><%=spiceSendLabel%></argument>
      <%
    }
    %>

  <!-- primary registry -->
  <argument>-registry</argument>
  <argument>http://www.dasregistry.org/registry/das1/sources/</argument>

  <!-- if the primary is down, fall back to this one -->
  <argument>-backupRegistry</argument>
  <argument>http://www.spice-3d.org/dasregistry/das1/sources/</argument>

    
  <% 
  // optional arguments 
   
   if (sendRasmolScript) {   
   %>
   <argument>-rasmolScript</argument>
   <argument><%=rasmolScript%></argument>
   <%
   }

   if (displayMessage){
   %>
   <argument>-displayMessage</argument>
   <argument><%=showMessage%></argument>
   <%   
   }

   if (displayMessageWidth != null ) {
    %>
   <argument>-displayMessageWidth</argument>
   <argument><%=displayMessageWidth%></argument>
   <%   
   }


   if (displayMessageHeight != null ) {
    %>
   <argument>-displayMessageHeight</argument>
   <argument><%=displayMessageHeight%></argument>
   <%   
   }

   if ( seqSelectStart != null ) {
    %>
   <argument>-seqSelectStart</argument>
   <argument><%=seqSelectStart%></argument>
   <%   
   }

   if ( seqSelectEnd != null ) {
    %>
   <argument>-seqSelectEnd</argument>
   <argument><%=seqSelectEnd%></argument>
   <%   
   }


   if ( pdbSelectStart != null ) {
    %>
   <argument>-pdbSelectStart</argument>
   <argument><%=pdbSelectStart%></argument>
   <%   
   }

   if ( seqSelectEnd != null ) {
    %>
   <argument>-pdbSelectEnd</argument>
   <argument><%=pdbSelectEnd%></argument>
   <%   
   }


   if ( localServerURL  != null ) {
   %>
   <argument>localServerURL</argument>
   <argument><%=localServerURL%></argument>
   <%
   }

   if ( localServerCoordSys  != null ) {
   %>
   <argument>localServerCoordSys</argument>
   <argument><%=localServerCoordSys%></argument>
   <%
   }


   if ( localServerName  != null ) {
   %>
   <argument>localServerName</argument>
   <argument><%=localServerName%></argument>
   <%
   }

   %>

  </application-desc>
 
</jnlp> 


